package net.facelib.sdk;

import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static net.gdface.utils.ConditionChecks.checkNotNull;

/**
 * 用户数据管理简单实现<br>
 * 应用层可以根据需要继承此类重写方法
 * @author guyadong
 *
 */
public class UserInfoManagement {
	
	/**
	 * featureId - userID 映射
	 */
	protected final Map<String, String> featureMap = new HashMap<>();
	/**
	 * userID - User 映射
	 */
	protected final Map<String, User> userMap = new HashMap<>();
	public UserInfoManagement() {
	}
	/**
	 * 添加用户信息
	 * @param userID 用户ID
	 * @param photo 用户照片数据,为{@code null}忽略
	 * @param description 用户信息描述,为{@code null}忽略
	 */
	void addUserInfo(String userID,URL photo,String description){
		synchronized (this) {
			if(null != userID ){
				userMap.put(userID, new UserInfo(userID, photo, description));
			}
		}
	}
	/**
	 * 更新用户人脸特征ID(人脸特征数据MD5校验码)
	 * @param userID 用户名
	 * @param featureId 人脸特征ID
	 */
	void updateUser(String userID,String featureId){
		synchronized (this) {
			User user = userMap.get(userID);
			if(null != user && null != featureId ){
				featureMap.put(featureId, userID);
				user.setFeatureId(featureId);
			}
		}
	}

	/**
	 * 根据特征MD5获取用户ID
	 * @param featureId 人脸特征ID
	 * @return 用户数据对象,用户不存在则返回{@code null}
	 */
	public User getUser(String featureId){
		synchronized (this) {
			if(null != featureId){
				String userID = featureMap.get(featureId);
				if(null != userID){
					return  userMap.get(userID);
				}
			}
			return null;
		}
	}
	public Collection<User> getAllUsers(){
		return userMap.values();
	}
	/**
	 * 根据用户ID删除指定用户
	 * @param userID
	 */
	public void removeUser(String userID){
		synchronized (this) {
			if(null != userID){
				User user = userMap.remove(userID);
				if(null != user){
					featureMap.remove(user.getFeatureId());
				}
			}
		}
	}
	/**
	 * 根据用户人脸特征的MD5删除指定的用户数据
	 * @param featureId
	 */
	public void removeFeature(String featureId){
		synchronized (this) {
			if(null != featureId){
				String userID = featureMap.remove(featureId);
				if(null != userID){
					userMap.remove(userID);
				}
			}
		}
	}
	/**
	 * 清除所有用户数据
	 */
	public void clear() {
		synchronized (this) {
			featureMap.clear();
			userMap.clear();
		}
	}
	/**
	 * 用户数据接口
	 * @author guyadong
	 *
	 */
	public static interface User{
		String getUserID();
		String getDescription();
		URL getPhoto();
		String getFeatureId();
		void setFeatureId(String featureId);
	}
	public static class UserInfo implements User{
		final String userID;
		final String description;
		final URL photo;
		String featureId;

		/**
		 * 构造方法
		 * @param userID 用户名(唯一)
		 * @param photo 用户标准照,基于此照片提取人脸特征用于用户识别 
		 * @param description 用户描述,为{@code null}忽略
		 */
		public UserInfo(String userID, URL photo, String description) {
			super();
			this.userID = checkNotNull(userID,"userID is null");
			this.photo = checkNotNull(photo," user photo is null");
			this.description = description;
		}
		public String getUserID() {
			return userID;
		}
		public String getDescription() {
			return description;
		}
		public URL getPhoto() {
			return photo;
		}
		@Override
		public void setFeatureId(String featureId) {
			this.featureId = featureId;
		}
		@Override
		public String getFeatureId() {
			return featureId;
		}
		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("UserInfo [userID=");
			builder.append(userID);
			builder.append(", description=");
			builder.append(description);
			builder.append(", photo=");
			builder.append(photo);
			builder.append(", featureId=");
			builder.append(featureId);
			builder.append("]");
			return builder.toString();
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((description == null) ? 0 : description.hashCode());
			result = prime * result + ((photo == null) ? 0 : photo.hashCode());
			result = prime * result + ((userID == null) ? 0 : userID.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (!(obj instanceof UserInfo))
				return false;
			UserInfo other = (UserInfo) obj;
			if (description == null) {
				if (other.description != null)
					return false;
			} else if (!description.equals(other.description))
				return false;
			if (photo == null) {
				if (other.photo != null)
					return false;
			} else if (!photo.equals(other.photo))
				return false;
			if (userID == null) {
				if (other.userID != null)
					return false;
			} else if (!userID.equals(other.userID))
				return false;
			return true;
		}
	}
}
